class Promise {
  // 构造方法
  constructor(executor) {
    this.PromiseState = 'pending';
    this.PromiseResult = null;

    const _this = this
    this.callbacks = []

    // resolve 函数
    function resolve(data) {
      if(_this.PromiseState !== 'pending') return
      _this.PromiseState = 'fulfilled';
      _this.PromiseResult = data;

      setTimeout(() => {
        _this.callbacks.forEach(item => {
          item.onResolved(data) 
        })
      })
    }

    // reject 函数
    function reject(data) {
      if(_this.PromiseState !== 'pending') return
      _this.PromiseState = 'rejected';
      _this.PromiseResult = data;

      setTimeout( () => {
        _this.callbacks.forEach(item => {
          item.onRejected(data) 
        })
      })
    }

    try {
      // 同步调用 【执行器函数】
      executor(resolve, reject);
    }catch(e){
      reject(e)
    }
  }

  // then 方法封装
  then(onResolved, onRejected) {
    const _this = this

    // 判断回调函数参数
    if (typeof onRejected !== 'function') {
      onRejected = reason => {
        throw reason
      }
    }

    if(typeof onResolved !== 'function') {
      onResolved = value => value
    }

    return new Promise( (resolve, reject) => {

      // 封装改变状态函数
      function callback(type) {
        try {
          let result = type(_this.PromiseResult)
          if (result instanceof Promise) {
            result.then( v => {
              resolve(v)
            }, r => {
              reject(r)
            })
          } else {
            resolve(result)
          }
        }catch(e) {
          reject(e)
        }
      }

      // 判断 当前Promise实例的 状态
      if (this.PromiseState === 'fulfilled') {
        setTimeout( () => {
          callback(onResolved)
        })
      }

      if (this.PromiseState === 'rejected') {
        setTimeout( () => {
          callback(onRejected)
        })
      }

      if (this.PromiseState === 'pending') {
        this.callbacks.push({
          onResolved: function() {
            callback(onResolved)
          },
          onRejected: function() {
            callback(onRejected)
          }
        })
      }

    })
  }

  // catch 方法封装
  catch(onRejected) {
    return this.then(undefined, onRejected);
  }

  // static 声明静态方法 不属于原型上的方法.
  // 添加 resolve 方法
  static resolve(value) {
    // 返回Promise 对象
    return new Promise( (resolve, reject) => {
      // 判断 传入的是不是 Promise
      if (value instanceof Promise) {
        value.then(v => {
          resolve(v)
        }, r => {
          reject(r)
        })
      }else {
        // 状态设置为成功
        resolve(value)
      }
    });
  }

  // 添加 reject 方法
  static reject(reason) {
    return new Promise( (resolve, reject) => {
      reject(reason)
    })
  }

  // 添加 all 方法
  static all(promises) {
    return new Promise( (resolve, reject) => {
  
      // 声明变量
      let count = 0;
      let arr = [];
  
      for( let i=0; i<promises.length; i++) {
        promises[i].then( v => {
          count++;
          arr[i] = v
          if(count === promises.length) resolve(arr)
        }, r => {
          reject(r)
        })
      }
    })
  }

  // 添加 race 方法封装
  static race(promises) {
    return new Promise( (resolve, reject) => {
      // 循环遍历
      promises.forEach( item => {
        item.then( v => {
          // 修改返回对象的状态为 【成功】
          resolve(v)
        }, r => {
          // 修改返回对象的状态为 【失败】
          reject(r)
        })
      })
    })
  }
}
